package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.ContractChange;
import com.qianyu.project.entity.ContractChangeList;
import com.qianyu.project.mapper.ContractChangeMapper;
import com.qianyu.project.service.IContractChangeListService;
import com.qianyu.project.service.IContractChangeService;
import com.qianyu.project.utils.PageUtil;
import com.qianyu.project.vo.ContractChangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ContractChangeServiceImpl extends ServiceImpl<ContractChangeMapper, ContractChange> implements IContractChangeService {
    @Autowired
    private ContractChangeMapper contractChangeMapper;
    @Autowired
    private IContractChangeListService contractChangeListService;

    @Override
    public String getMaxId() {
        String id = contractChangeMapper.getMaxId();
        if(id == null){
            id = DateUtil.today().replaceAll("-", "") + "000";
        }
        return id;
    }

    @Transactional
    @FlowBegin("contract_change")
    @Override
    public Integer saveBoth(@FlowEntry("id") ContractChange changeForm, List<ContractChangeList> changeDetails) {
        changeForm.setApproveStatus("1");
        contractChangeMapper.insert(changeForm);
        contractChangeListService.saveBatch(changeDetails);
        return 1;
    }

    @Override
    public IPage<ContractChangeVo> getList(Map<String, String> params) {
        return contractChangeMapper.getList(PageUtil.createPage(params), params);
    }

    @Override
    public ContractChangeVo selectOne(String id) {
        return contractChangeMapper.getOne(id);
    }


}
